package com.runfig.work.job;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.runfig.work.domain.FileCustom;
import com.runfig.work.mapper.FileCustomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Component
public class OneJob implements SimpleJob {

    @Autowired
    private FileCustomMapper fileCustomMapper;

    @Override
    public void execute(ShardingContext shardingContext) {

         work(shardingContext.getShardingParameter());
    }

    private void work(String type) {
        List<FileCustom> fileCustoms = fileCustomMapper.selecByType(type);
        //System.out.println(fileCustoms);

//        fileCustoms = fileCustoms.stream().map((cao)->{
//            cao.setBackedUp(true);
//            System.out.println("ddddddd");
//            return cao;
//        }).collect(Collectors.toList());

        for (FileCustom fileCustom : fileCustoms) {
            backUpFile(fileCustom);
        }

    }


    private void backUpFile(FileCustom fileCustom){
        try {
            //模拟备份动作
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(fileCustom.getId()+"-"+fileCustom.getType()+"-"+fileCustom.getName());
        fileCustomMapper.changeState(fileCustom.getId(),1);
    }
}
